package pp.infrastructure.mysql.mapper.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import pp.infrastructure.exception.PPException;
import pp.infrastructure.exception.PPExceptionInfo;

import java.util.List;

@Repository
@Slf4j
public class UserReader {

    private final UserMapper mapper;

    public UserReader(@Autowired UserMapper mapper) {
        this.mapper = mapper;
    }

    public UserDO getById(long id) {
        if(id <= 0) {
            log.error("{}.{} --> {};", this.getClass().getName(), new Object(){}.getClass().getEnclosingMethod().getName()
                    , "根据 ID 查询 User，输入 ID 无效，id = " + id);
            throw new PPException(PPExceptionInfo.GENERIC_SYSTEM_INNER_PARAM_INVALID);
        }
        return mapper.selectById(id);
    }

    public UserDO getByAccount(String account) {
        if(account == null || account.isEmpty()) {
            log.error("{}.{} --> {};", this.getClass().getName(), new Object(){}.getClass().getEnclosingMethod().getName()
                    , "根据账号查询 User，输入账号无效，account = " + account);
            throw new PPException(PPExceptionInfo.GENERIC_SYSTEM_INNER_PARAM_INVALID);
        }
        LambdaQueryWrapper<UserDO> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserDO::getAccount, account);
        return mapper.selectOne(lqw);
    }

    public List<UserDO> getSubUsersByMainUserId(long mainUserId) {
        if(mainUserId <= 0) {
            log.error("{}.{} --> {};", this.getClass().getName(), new Object(){}.getClass().getEnclosingMethod().getName()
                    , "根据主用户 ID 查询子用户列表，输入 mainUserId 无效，mainUserId = " + mainUserId);
            throw new PPException(PPExceptionInfo.GENERIC_SYSTEM_INNER_PARAM_INVALID);
        }
        LambdaQueryWrapper<UserDO> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(UserDO::getMainUserId, mainUserId);
        return mapper.selectList(lqw);
    }

}
